//
//  KSSupplierManageViewController.swift
//  Supermarket
//
//  Created by liuxm on 2017/9/28.
//  Copyright © 2017年 kushao. All rights reserved.
//

import UIKit
import MJRefresh

/// 供应商管理
class KSSupplierManageViewController: KSBaseViewController {
    var tableview: UITableView!
    private var dataArray: [KSSupplierManageDataListModel] = []
    private var page: Int = 1
    
    private lazy var notHaveDataExistView: KSNotHaveDataExistView = {
        let view = KSNotHaveDataExistView(frame: self.view.bounds)
        view.initView(imageName: "Common_CollectionEmpty", content: "暂无供应商")
        
        return view
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // Do any additional setup after loading the view.
        
        self.title = "供应商管理"
        tableview = UITableView()
        tableview.register(KSSupplierManageTableViewCell.self, forCellReuseIdentifier: "KSSupplierManageTableViewCell")
        tableview.backgroundColor = KSColor.kColorViewBackground
        tableview.separatorStyle = .none
        tableview.delegate = self
        tableview.dataSource = self
        self.view.addSubview(tableview)
        
        tableview.snp.makeConstraints { (make) in
            make.edges.equalTo(UIEdgeInsets.init(top: 0, left: 0, bottom: 0, right: 0))
        }
        
        tableview.mj_header = MJRefreshNormalHeader.init(refreshingTarget: self, refreshingAction: #selector(loadNewData))
        tableview.mj_footer = MJRefreshAutoNormalFooter.init(refreshingTarget: self, refreshingAction: #selector(loadMoreData))
        tableview.mj_footer.isHidden = true
        tableview.estimatedRowHeight = 80
        
        self.startLoading()
        self.loadNewData()
    }
    
    
    @objc func loadNewData() {
        self.page = 1
        self.requestData()
    }
    
    @objc private func loadMoreData() {
        if !(tableview.mj_header.state == MJRefreshState.refreshing) {
            self.page += 1
            self.requestData()
        }
    }
    
    private func requestData() {
        self.request.requestGetSellerStorage(page: "\(self.page)")
    }
    
    override func requestSuccess(result: String, apiType: KSApiType) {
        super.requestSuccess(result: result, apiType: apiType)
        
        let model = KSSupplierManageModel.deserialize(from: result)
        if (model?.data?.list?.count)! > 0 {
            if self.page == 1 {
                dataArray.removeAll()
            }
            dataArray = dataArray + (model?.data?.list)!
        } else {
            if self.page > 1 {
                self.page -= 1
            }
        }
        
        tableview.reloadData()
        tableview.mj_header.endRefreshing()
        tableview.mj_footer.endRefreshing()
        
        if dataArray.count == 0 {
            self.view.addSubview(self.notHaveDataExistView)
        }
    }
    
    override func requestFaile(code: Int, message: String, apiType: KSApiType) {
        super.requestFaile(code: code, message: message, apiType: apiType)
        if self.page > 1 {
            self.page -= 1
        }
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

extension KSSupplierManageViewController: UITableViewDelegate,UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dataArray.count
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return  80
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "KSSupplierManageTableViewCell", for: indexPath) as! KSSupplierManageTableViewCell
        cell.setDataWithModel(model: dataArray[indexPath.row])
        return cell
    }
}
